package com.hca.training.zk.javabasic.Stringtest.Stringtest;

import java.text.Format;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class TestDate {
    public  static  void main(String[] arg0)
    {
//        getnowtime();//获取当前系统时间Calendar对象
//        chengetime();
//        resolve();
        compare();
    }
    /**
     * 1.获取当前系统时间Calendar对象
     * 2.指定日期
     * assign
     */
    public static void getnowtime()
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar a=Calendar.getInstance();
        System.out.println(sdf.format(a.getTime()));
        a.set(2027,10,20);
        System.out.println(sdf.format(a.getTime()));
    }

    /**
     *3.加减月份、加减天数、加减小时、加减分钟、加减秒数
     * 4.格式已经定义
     */
    public static void chengetime()
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar rightNow = Calendar.getInstance();
        rightNow.set(2017,10,1);
        rightNow.add(Calendar.YEAR,-8);//日期减1年
        rightNow.add(Calendar.MONTH,1);//日期加3个月
        rightNow.add(Calendar.DAY_OF_YEAR,-1);//日期加10天
        System.out.println(rightNow.getActualMaximum(Calendar.DATE));
        String reStr = sdf.format(rightNow.getTime());
        System.out.println(reStr);


    }
    /**
     *5.	将日期字符解析为Calendar对象
     */
    public static void resolve()
    {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
        String a="2016/10/20";
        try {
            Date b=sdf.parse(a);
            Calendar rightNow = Calendar.getInstance();
            rightNow.setTime(b);
            String reStr = sdf.format(rightNow.getTime());
            System.out.println(reStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }



    }
    /**
     * 6.比较两个日期早晚7.8.9.10
     */
    public static void compare()
    {
        //方式一
        Calendar a= Calendar.getInstance();
        a.set(2011, 05,28, 19,50, 2);
        //参数为年 月 日 时 分 秒
        a.set(Calendar.MILLISECOND, 0);
        //设置毫秒
        Calendar c= Calendar.getInstance();
        c.set(2011, 05,28, 19,50, 3);
        c.set(Calendar.MILLISECOND, 0);
        System.out.println(a.compareTo(c));//判断毫秒值
        //a比c早,返回-1,
        //a与c相同,返回0
        //a比c晚,返回1

        //方式二
        String startTime = "2012-12-12 12:45:39";
        String endTime = "2012-12-12 12:45:40";
        SimpleDateFormat sdfs = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date startDate = null;
        try {
            startDate = sdfs.parse(startTime);
            Date endDate = sdfs.parse(endTime);
            Calendar start = Calendar.getInstance();
            Calendar end = Calendar.getInstance();
            start.setTime(startDate);
          startDate=start.getTime();                             //7.
            System.out.println("输出下相互转换："+startDate);
            end.setTime(endDate);
            if(start.before(end)){
                System.out.println("开始时间小于结束时间");
            }else if(start.after(end)){
                System.out.println("开始时间大于结束时间");
            }else if(start.equals(end)){
                System.out.println("开始时间等于结束时间");
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //8.
        Calendar now= Calendar.getInstance();
        System.out.println(now.getTime());
        System.out.println(now.getTimeInMillis());//毫秒
        System.out.println(System.nanoTime());//纳秒

        //9.
        SimpleDateFormat ss = new SimpleDateFormat("yyyy-MM-dd");
        Calendar n= Calendar.getInstance();
        System.out.println(n.getTime());
        System.out.println(n.getActualMaximum(Calendar.DATE));//当月最后一天
        System.out.println(ss.format(n.getTime()));
        //10.
        try {
            SimpleDateFormat f=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date d1=f.parse("2012-09-08 10:10:10");
            Date d2= f.parse("2012-09-10 00:00:00");
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
            d1=sdf.parse(sdf.format(d1));
            d2=sdf.parse(sdf.format(d2));
            Calendar cal = Calendar.getInstance();
            cal.setTime(d1);
            long time1 = cal.getTimeInMillis();
            cal.setTime(d2);
            long time2 = cal.getTimeInMillis();
            long between_days=(time2-time1)/(1000*3600*24);
            System.out.println("相差天数:"+Integer.parseInt(String.valueOf(between_days)));

        } catch (ParseException e) {
            e.printStackTrace();
        }



    }
}
